package com.zjp.plugins;

import cn.hutool.core.text.NamingCase;
import org.apache.ibatis.executor.resultset.ResultSetHandler;
import org.apache.ibatis.plugin.Interceptor;
import org.apache.ibatis.plugin.Intercepts;
import org.apache.ibatis.plugin.Invocation;
import org.apache.ibatis.plugin.Signature;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.sql.Statement;
import java.util.HashSet;
import java.util.List;
import java.util.Locale;
import java.util.Map;

@Intercepts({
        @Signature(
                type = ResultSetHandler.class, method = "handleResultSets"
                , args = {Statement.class

        }
        )
})
@SuppressWarnings({"unchecked", "rawtypes"})
public class CameHumpInterceptor implements Interceptor {
    private final Logger log = LoggerFactory.getLogger(this.getClass());

    @Override
    public Object intercept(Invocation invocation) throws Throwable {
        List<Object> list = (List<Object>) invocation.proceed();

        for (Object object : list) {
            if (object instanceof Map) {
                log.info("==命名转换==");
                processMap((Map) object);
            } else {
                break;
            }
        }
        return list;
    }


    public void processMap(Map<String, Object> map) {
        HashSet<String> hashSet = new HashSet<>(map.keySet());
        for (String s : hashSet) {
            Object o = map.get(s);
            map.remove(s);
            map.put(NamingCase.toCamelCase(s.toLowerCase(Locale.ROOT)), o);
        }
    }
}



